precision mediump float;
uniform sampler2D u_texture;

uniform vec3 u_camera_pos;
uniform vec4 u_light_pos0;

uniform vec4 u_light_diffuse0;
uniform vec4 u_light_ambient0;

uniform mat4 u_worldkMatrix;
uniform mat4 u_mvpMatixL;

// light source
uniform float u_light_constantAttenuation;
uniform float u_light_linearAttenuation;
uniform float u_light_quadraticAttenuation;

// material
uniform float u_material_shininess;
uniform vec4 u_material_specular;

varying vec4 vColor;
varying vec2 v_uv;
varying vec4 v_NormalW;
varying vec4 v_posW;

void main()
{	
	vec4 ObjColor = vec4(0.5, 0.0, 0.0, 1.0);
	//vec4 ObjColor = texture2D(u_texture, v_uv);;
	
	//normalize vector normal
	vec3 normalVector = normalize(v_NormalW.xyz);
	
	//Get light vector L 
	vec3 LightVector = normalize(u_light_pos0.xyz - v_posW.xyz);
	float distance = length(u_light_pos0 - v_posW);
	float attenuation = 1.0 /(u_light_constantAttenuation  + u_light_linearAttenuation * distance + u_light_quadraticAttenuation * distance * distance);

	//Calculate dot product of N and L
	float NdotL = max(0.0, dot(normalVector, LightVector));
	
	vec4 v_diffuse = u_light_diffuse0 *  NdotL * attenuation;	
	vec4 v_ambient = u_light_ambient0;
	
	//Get eye vector E for each vertex 

	vec3 v_toEye =   v_posW.xyz - u_camera_pos;
	v_toEye = normalize(v_toEye);
	 
	//Get reflect vector R (L reflect N) 
	vec3 ReflectVector = normalize(reflect(LightVector,normalVector)); 
	 
	//Compute dot product of R and E 
	float RdotE = max(0.0, dot(v_toEye, ReflectVector)); 
	 
	//Compute reflect power  
	float ReflectPower = pow(RdotE, u_material_shininess); 
	 
	//Compute Specular 
	vec4 Specular = clamp(u_material_specular * ReflectPower, 0.0, 1.0) * attenuation; 
	
	gl_FragColor =  ObjColor * (v_diffuse + v_ambient ) * attenuation + Specular;
		
	//gl_FragColor = vec4(0.8, 0.0, 0.0, 1.0);
	
}

